package com.xy.weatherDate;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 谢阳
 * @version 1.8.0_131
 * @date 2022/6/19 9:55
 * @description
 */
public class WeatherData implements Subject {
    // 观察者集合
    private List<Observer> observers = new ArrayList<>();
    // 天气信息
    private float temperature; // 温度
    private float humidity; // 湿度
    private float pressure; // 气压

    @Override
    public void registerObserver(Observer observer) {
        observers.add(observer);
    }

    @Override
    public void removeObserver(Observer observer) {
        observers.remove(observer);
    }

    @Override
    public void notifyObservers() {
        observers.forEach(Observer::update);
    }

    /**
     * 获取气象信息
     */
    private void measurementsChanged() {
        // 通知所有观察者
        notifyObservers();
    }

    /**
     * 模拟气象站获取信息
     */
    public void setMeasurements(float temperature, float humidity, float pressure) {
        this.temperature = temperature;
        this.humidity = humidity;
        this.pressure = pressure;
        measurementsChanged();
    }

    public float getTemperature() {
        return temperature;
    }

    public float getHumidity() {
        return humidity;
    }

    public float getPressure() {
        return pressure;
    }
}
